const { Admin } = require("../model/index");
const { getAvater } = require("./homeInfoServer")
const { avater1 } = require('../routerConfig')
exports.login =  async(loginId,loginPassw)=>{
   if(loginId && loginPassw){
        const result = await Admin.find({
            loginId,
            loginPassw
        },'loginId');

        if(result.length <= 0){
            return false;
        }
        const avatar =  await Avater(avater1)
        return {
            loginId : result[0].loginId,
            token : "",
            avatar,
            _id : result[0]._id.toString()
        };

   }else{
       return false;
   }
    
}

exports.whyi = async (_Id)=>{
    if(!_Id) return null;
    const result = await Admin.findById(_Id);
    if(result){
        const avatar = await Avater(avater1)
        return {
            loginId :result.loginId,
            avatar
        };
    }else{
        return 'Id is null';
    }
}

exports.addAdmin = async (loginId,loginPassw)=>{

    if(loginId && loginPassw){

        const result = await Admin.create({
            loginId,
            loginPassw
        })

        return result;
    }else{
        return null;
    }

}
async function Avater(avater1){
    const avater = await getAvater(avater1);
    return (()=> avater.img)()
}
exports.finds = async ()=>{
    const result =await Admin.find();
    
    if(result.length <= 0){

        return false
    }else{
        return true;
    }  

}